/**
 * Copyright (C) 2015-2018 IQIYI
 * All rights reserved.
 *
 * AUTHOR(S)
 * 王海斌
 *   E-mail: wanghaibin@qiyi.com
 * 
 * VERSION
 *   Wed 20 Jun 2018 03:15:08 PM CST
 */

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <vector>
#include <string.h>

char buffer[] = { "A text record to be written" };
typedef struct dataS
{
    int index;
    size_t start;
    size_t len;
    size_t cur;

    dataS(){
        index=0;
        start=0;
        len=0;
        cur=0;
    }
    void out(){
        printf(" index=%d, start=%zd, len=%zd, cur=%zd \n", index, start, len, cur);
    }
}dataS;

void insert_struct(std::vector<dataS> & da)
{
    for(int i=0; i<5; ++i) {
        dataS data1;
        data1.index = i;
        data1.start=i*200;
        data1.len = 200;
        data1.cur = 50+i*10;
        da.push_back(data1);
    }
}

int write_data( const char * path )
{
    int  fd;
    int  size_written;
    fd = creat(path, S_IRUSR | S_IWUSR );
    std::vector<dataS> vec;
    insert_struct(vec);
    int iSize = vec.size();
    size_written = write( fd, &iSize,sizeof( iSize ) );
    printf("write=%d, \n", size_written);
    for(int i=0; i<vec.size(); ++i) {
        size_written = write( fd, &vec[i],sizeof( dataS ) );
        if( size_written != sizeof( dataS ) ) {
            perror( "Error writing myfile.dat" );
            return EXIT_FAILURE;
        }

        printf("write ");
        vec[i].out();
    }

    close( fd );
}

int write_data2( const char * path )
{
    int  fd;
    int  size_written;
    fd = creat(path, S_IRUSR | S_IWUSR );
    std::vector<dataS> vec;
    insert_struct(vec);
    int iSize = vec.size();
    char * buf = (char*)malloc(sizeof(int)+iSize*sizeof(dataS));
    memcpy(buf,&iSize, sizeof(int));
    for(int i=0; i<vec.size(); ++i) {
        memcpy(buf+sizeof(int)+i*sizeof(dataS), &vec[i], sizeof(dataS));
        printf("write ");
        vec[i].out();
    }
    size_written = write( fd, buf, sizeof(int)+iSize*sizeof(dataS));
    free(buf);
    if( size_written != (sizeof(int)+iSize*sizeof(dataS))) {
        perror( "Error writing myfile.dat" );
        return EXIT_FAILURE;
    }
    close( fd );
}

int read_data2(const char * path)
{
    int fd;
    int size_read;
    fd = open(path, O_RDONLY );
    int iSize = 0;
    size_read = read( fd, &iSize, sizeof( int ) );
    char * buf = (char*)malloc(iSize*sizeof(dataS));
    size_read = read( fd, buf, iSize*sizeof(dataS));
    if( size_read == -1 ) {
        return EXIT_FAILURE;
    }
    for(int i=0; i<iSize; ++i) {
        dataS data1;
        memcpy(&data1, buf+i*sizeof(dataS), sizeof(dataS));
        printf("2read ");
        data1.out();
    }
    free(buf);

    close( fd );
}

int read_data(const char * path)
{
    int fd;
    int size_read;
    fd = open(path, O_RDONLY );
    int iSize = 0;
    size_read = read( fd, &iSize, sizeof( int ) );

    for(int i=0; i<iSize; ++i) {
        dataS data1;
        size_read = read( fd, &data1, sizeof( dataS ) );

        if( size_read == -1 ) {
            return EXIT_FAILURE;
        }
        printf("read ");
        data1.out();
    }

    close( fd );
}

int main(int argc, char *argv[])
{
    const char * file="myfile.dat";
    write_data2(file);
    read_data2(file);
    return 0;
}
